package com.xiaxueqi.web.home;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.dao.Page;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.CRUDActionSupport;
import com.niagara.web.struts2.Struts2Utils;
import com.xiaxueqi.entity.book.BookForsale;
import com.xiaxueqi.entity.book.BookStore;

@Results( { @Result(name = CRUDActionSupport.RELOAD, location = "book-manager.action", type = "redirect") })
public class BookManagerAction extends CRUDActionSupport<BookForsale> {
	private static final long serialVersionUID = 1828145608456527558L;
	@Autowired
	private BaseService baseService;
	private BookForsale entity;
	private Integer id;
	private List<String> bookName = new ArrayList<String>();
	private Page<BookForsale> page;

	private UserDetail detail = SecurityContext.getUserDetail();
	private Integer userId = detail != null ? Integer.valueOf(detail
			.getUserId().intValue()) : null;

	public Page<BookForsale> getPage() {
		return page;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	public List<String> getBookName() {
		return this.bookName;
	}

	// 用户自己的图书逻辑删除操作
	@Override
	public String delete() throws Exception {
		this.entity = this.baseService.get(BookForsale.class, this.id);
		this.entity.setDeleted("Y");
		this.baseService.update(this.entity);
		return RELOAD;
	}

	// 用户自己书库的管理列表，包括未上架和在架销售的图书
	@SuppressWarnings("unchecked")
	@Override
	public String list() throws Exception {
		Map map = Struts2Utils.getParameters();
		Integer pageNo = Struts2Utils.getIntParameter(PARAMS_PAGENO, Integer
				.valueOf(1));
		map.put("ownerId", userId);
		this.page = this.baseService.findForPaging(BookForsale.class, map,
				pageNo, 10);
		// 取书名
		List<BookForsale> books = this.page != null ? this.page.getPageList()
				: new ArrayList<BookForsale>();
		for (int i = 0; i < books.size(); i++) {
			this.bookName.add(this.baseService.get(BookStore.class,
					books.get(i).getId()).getBookName());
		}
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (this.id != null) {
			this.entity = this.baseService.get(BookForsale.class, this.id);
		} else {
			this.entity = new BookForsale();
		}
	}

	// 用来操作上架、下架书的管理(更新status字段，切换status字段的值{onsale,unsale})
	public String change() throws Exception {
		if (this.id != null) {
			this.entity = this.baseService.get(BookForsale.class, this.id);
			if (this.entity.getStatus().equals("onsale")) {
				this.entity.setStatus("unsale");
				this.baseService.update(this.entity);
			} else {
				this.entity.setStatus("onsale");
				this.baseService.update(this.entity);
			}
		}
		return RELOAD;
	}
	@Override
	public String save() throws Exception {
		return RELOAD;
	}

	public BookForsale getModel() {
		return this.entity;
	}
}
